use proc_macro2::TokenStream;
use quote::quote;
use proc_macro2::{Ident, Span};


pub fn check( fnc: TokenStream) ->TokenStream{
    quote!{
       match #fnc  {
            Ok(r)=> r,
            Err(e) => {
                env.throw_this_error(e);
                return core::ptr::null_mut();
            }
        }
    }
}


pub fn check_rust_func( fnc: TokenStream) ->TokenStream{
    quote!{
       match #fnc  {
            Ok(r)=> r,
            Err(e) => {
                env.throw_error("Internal", e.to_string().as_str());
                return core::ptr::null_mut();
            }
        }
    }
}


pub fn check_with_let_return(return_name: &str, fnc: TokenStream) ->TokenStream{
    let name = Ident::new(return_name, Span::call_site());
    let ctx = check(fnc);
    quote!{
        let #name = #ctx ;
    }
}
pub fn check_rust_fn_with_let_return(return_name: &str, fnc: TokenStream) ->TokenStream{
    let name = Ident::new(return_name, Span::call_site());
    let ctx = check_rust_func(fnc);
    quote!{
        let #name = #ctx ;
    }
}
